package com.liukai.todo_android.entity.data;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.liukai.todo_android.common.utils.DateUtil;
import com.liukai.todo_android.entity.ItemVo;
import com.liukai.todo_android.entity.ItemVo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class DataTree<K, V> {

    private K groupItem;
    private List<V> subItems;

    public DataTree() {

    }

    public DataTree(K groupItem, List<V> subItems) {
        this.groupItem = groupItem;
        this.subItems = subItems;
    }

    public K getGroupItem() {
        return groupItem;
    }

    public List<V> getSubItems() {
        return subItems;
    }

    public DataTree<K, V> setGroupItem(K groupItem) {
        this.groupItem = groupItem;
        return this;
    }

    public DataTree<K, V> setSubItems(List<V> subItems) {
        this.subItems = subItems;
        return this;
    }

    @Override
    public String toString() {
        return "DataTree{" +
                "groupItem=" + groupItem +
                ", subItems=" + subItems +
                "}\n";
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public List<DataTree<HistoryDate, ItemVo>> createDateTree(List<ItemVo> itemVoList) {
        List<DataTree<HistoryDate, ItemVo>> list = createGroup(itemVoList);
        createSub(itemVoList, list);
        return list;

    }

    /**
     * 创建二级标题
     *
     * @param ItemVoList
     * @param list
     */
    private void createSub(List<ItemVo> ItemVoList, List<DataTree<HistoryDate, ItemVo>> list) {
        String date;
        for (ItemVo ItemVo : ItemVoList) {

            date = DateUtil.getYearMonthDay(ItemVo.getGmtFinished());
            for (DataTree<HistoryDate, ItemVo> ItemVoDataTree : list) {
                if (ItemVoDataTree.getGroupItem().getDate().equals(date)) {
                    ItemVoDataTree.getSubItems().add(ItemVo);
                    ItemVoDataTree.getGroupItem().countAdd();
                }

            }

        }
    }

    /**
     * 创建一级标题
     *
     * @param ItemVoList
     * @return
     */
    private List<DataTree<HistoryDate, ItemVo>> createGroup(List<ItemVo> ItemVoList) {
        DataTree<HistoryDate, ItemVo> dataTree = null;
        List<DataTree<HistoryDate, ItemVo>> list = new ArrayList<>();
        List<ItemVo> ItemVos = null;
        String date = null;
        for (ItemVo ItemVo : ItemVoList) {
            dataTree = new DataTree<>();
            ItemVos = new ArrayList<>();
            HistoryDate historyDate = new HistoryDate();
            date = DateUtil.getYearMonthDay(ItemVo.getGmtFinished());
            historyDate.setDate(date);

            if (!validateCreateGroup(list, date)) {
                continue;
            }

            dataTree.setGroupItem(historyDate)
                    .setSubItems(ItemVos);
            list.add(dataTree);
        }
        Collections.sort(list, Comparator
                .comparing(DataTree<HistoryDate, ItemVo>::getGroupItem,
                        (o1, o2) -> o2.getDate().compareTo(o1.getDate())));
        return list;
    }

    /**
     * 校验能不能创建一级标题的数据
     *
     * @param list
     * @param date
     * @return
     */
    private boolean validateCreateGroup(List<DataTree<HistoryDate, ItemVo>> list, String date) {
        for (DataTree<HistoryDate, ItemVo> tree : list) {
            if (tree.getGroupItem().getDate().equals(date)) {
                return false;
            }
        }
        return true;
    }

}